/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package frontController;

import businessDelegates.ClienteBusinessDelegate;
import businessDelegates.PaisBusinessDelegate;
import businessObjects.Cliente;
import businessObjects.Pais;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author williamprado
 */
@WebServlet(name="registroFrontController", urlPatterns={"/registroFrontController"})
public class registroFrontController extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String action = request.getParameter("action");
        HttpSession sesionOK = request.getSession();
        //HttpSession currentSession = request.getSession();
        /*if(currentSession == null){
            response.sendRedirect("index.jsp");
        }else{*/
            if(action==null){
                sesionOK.setAttribute("error", "Lo siento ha ocurrido un error al intentar crear un usuario,<br>Por favor intente nuevamente.");
                response.sendRedirect("error.jsp");
            }
            if(action.equals("toreg")){
                List<Pais> lp = null;
                PaisBusinessDelegate pbd = new PaisBusinessDelegate();
                try{
                    lp = pbd.todosLosPaises();
                    sesionOK.setAttribute("listapaises", lp);
                    response.sendRedirect("registro.jsp");
                }catch(Exception e){
                    e.printStackTrace();
                    response.sendRedirect("index.jsp");
                }

            }
            if(action.equals("regNew")){
                Cliente c = new Cliente();
                Pais p = new Pais();
                PaisBusinessDelegate pbd = new PaisBusinessDelegate();
                p = pbd.buscarPaisPorNombre(request.getParameter("pais"));
                try{
                    c.setLogin(request.getParameter("login"));
                    c.setPassword(request.getParameter("password"));
                    c.setEmail(request.getParameter("email"));
                    c.setNombre(request.getParameter("nombre"));
                    c.setPais(p);
                    c.setCiudad(request.getParameter("ciudad"));
                    }catch(Exception e){
                    sesionOK.setAttribute("message", "Registro fallido armando kosas");                    
                }
                try{
                    ClienteBusinessDelegate cbd = new ClienteBusinessDelegate();
                    cbd.registrarCliente(c);

                    sesionOK.setAttribute("message", "Registro exitoso");                    
                }catch(Exception e){
                    sesionOK.setAttribute("message", "Registro fallido guardando");
                }
                response.sendRedirect("registro.jsp");
            }
        //}

        /*
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet registroFrontController</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet registroFrontController at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
        */
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
